Tutorials / Move your Player / Spielfigur bewegen
Written by bilou84, Translation edited by Pmikro. Hallo zusammen! Willkommen zu meinem ersten Tutorial. Ich hoffe es ist das erste von vielen! Für die, die mich nicht kennen, ich war an der Erstellung von folgenden Games beteiligt: Porte par le Vent, Doomsday Carrot Rampage und CraftFighter. Meine Spezialität ist das scripting, worum es in diesem Tutorial geht. Mein Ziel ist es, euch zu zeigen wie das scripting in Craftstudio funktioniert. Ich möchte euch helfen die funktionsweise der API zu verstehen und wie ihr sie anwendet. Ich möchte nicht komplett am Anfang beginnen, bitte schaut euch das Wiki und die Spiele die schon erstellt wurden, an. Erste Schritte Du hast also deinen ersten Charakter mit viel Liebe fertiggestellt und fragst dich nun wie du ihn bewegen kannst? Nun, das ist nicht so kompliziert: local MOVE_SPEED = 0.1 local ROTATE_SPEED = 2 function Behavior:Awake() end function Behavior:Update() local horizontal = CraftStudio.Input.GetAxisValue( "Horizontal" ) local vertical = CraftStudio.Input.GetAxisValue( "Vertical" ) self.gameObject.transform:Rotate( Quaternion:FromAxisAngle( Vector3:Up(), horizontal*ROTATE_SPEED) ) self.gameObject.transform:MoveOriented( vertical*Vector3:Forward()*MOVE_SPEED ) end Damit es funktioniert, musst du 2 Achsen im Administrator- Tab definieren : horizontal und vertikal. Für die horizontale Achse: A für positiv und D für negativ. Für die vertikale Achse: W für positiv und S für negativ. Dann muss das Skript nur noch an das Objekt gehangen werden, welches bewegt werden soll. Ich benutze Q und E um das Objekt zu rotieren, W und S um es zu bewegen. Ok. Wie funktioniert es nun? Am Anfang haben wir folgendes: local MOVE_SPEED = 0.1 local ROTATE_SPEED = 2 Diese beiden Konstanten machen das Skript leichter lesbar. These two are constants, which make your script easier to read. You must avoid having stray numbers that you don't understand the meaning. You could just put "0.1" in every place where you wrote "MOVE_SPEED" in the script to save yourself this step, however then you would have to change the "0.1" in every spot it was used if you ever wanted to change it, and also other people reading your code may not understand it. Name your constants appropriately to what they do! Next we have the Awake which is empty. We don't need anything particular for this code since everything we're going to be doing will happen in a loop, so since? Awake? ''only happens once, we don't have a use for it. Finally the ''Update : local horizontal = CraftStudio.Input.GetAxisValue( "Horizontal" ) local vertical = CraftStudio.Input.GetAxisValue( "Vertical" ) We use these lines to check if you pressed the keys. For instance, when you pressed Z, vertical will become 1. Then self.gameObject.transform:Rotate( Quaternion:FromAxisAngle( Vector3:Up(), horizontal*ROTATE_SPEED) ) This line make your object rotate. self refers to the object the script is attached to. gameObject give acces to the object itself. transform allow you to use the attributes toward the displacement of the object. First we will use Rotate. This one make your object rotate, given a Quaternion in parameters. A Quaternion is a weird object which reflects a rotation. You can create it from an axis and the magnitude of the rotation with the attribut FromAxisAngle. Here we use the vertical axis with Vector3:Up() and we have already saw the rotation magnitude with ROTATE_SPEED. We can also see that the constant was multiplied by horizontal. We don't want to make our player move constantly. So when we don't press the key, horizontal equal to 0, so the net movement evaluates to 0. Last line : self.gameObject.transform:MoveOriented( vertical*Vector3:Forward()*MOVE_SPEED ) The beginning is the same but this time we use the attribut MoveOriented. With this script, you can move your object according to its orientation. It uses the Vector3:Forward() to move forward. Once more, there is the constant which contains the speed and it is multiplied by vertical in order to move when you press a button. Conclusion Done! I hope my explanations are clear enough and my english as well. (I'm French) You can see here a video to illustrate this tutorial : Video ? It's in french but there are subtitles. And here is a link to the project source if you want to test it by yourself:? Pack Thanks you for reading and feel free to leave comments ! Bilou84